package org.tio.site.controller.manager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.maintain.IpStats;
import org.tio.http.common.HttpConfig;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.http.server.annotation.RequestPath;
import org.tio.http.server.util.Resps;
import org.tio.utils.resp.RespVo;
import org.tio.utils.ui.layui.table.LayuiPage;

/**
 * @author tanyaowu
 * 2017年6月29日 下午7:53:59
 */
@RequestPath(value = "/m/tio")
public class TioController {
	private static Logger log = LoggerFactory.getLogger(TioController.class);

	/**
	 * @param args
	 * @author tanyaowu
	 */
	public static void main(String[] args) {

	}

	/**
	 *
	 * @author tanyaowu
	 */
	public TioController() {
	}

	@RequestPath(value = "/clearStaticResCache")
	public HttpResponse clearStaticResCache(HttpRequest request, HttpConfig httpConfig) throws Exception {
		try {
			httpConfig.getHttpRequestHandler().clearStaticResCache(request);
			HttpResponse ret = Resps.json(request, RespVo.ok("缓存已经清空"));
			return ret;
		} catch (Exception e) {
			log.error(e.toString(), e);
			return Resps.json(request, RespVo.fail());
		}
	}

	/**
	 * 获取ipStat信息
	 * @param request
	 * @param channelContext
	 * @return
	 * @throws Exception
	 * @author: tanyaowu
	 */
	@RequestPath(value = "/ipStat")
	public HttpResponse ipStat(Long duration, HttpRequest request, ChannelContext channelContext) throws Exception {
		try {
			IpStats ipStats = channelContext.getGroupContext().ipStats;
			HttpResponse ret = Resps.json(request, LayuiPage.ok(ipStats.values(duration), ipStats.size(duration)));
			return ret;
		} catch (Exception e) {
			log.error(e.toString(), e);
			return Resps.json(request, RespVo.fail());
		}
	}
	
	/**
	 * 获取所有可统计的时长
	 * @param duration
	 * @param request
	 * @param channelContext
	 * @return
	 * @throws Exception
	 * @author: tanyaowu
	 */
	@RequestPath(value = "/allIpStat")
	public HttpResponse allIpStat(Long duration, HttpRequest request, ChannelContext channelContext) throws Exception {
		try {
			IpStats ipStats = channelContext.getGroupContext().ipStats;
			HttpResponse ret = Resps.json(request, ipStats.durationList);
			return ret;
		} catch (Exception e) {
			log.error(e.toString(), e);
			return Resps.json(request, RespVo.fail());
		}
	}
	
//	@RequestPath(value = "/ipStat")
//	public HttpResponse ipStat(HttpRequest request, ChannelContext channelContext) throws Exception {
//		try {
//			Ips ips = channelContext.getGroupContext().ips;
//			HttpResponse ret = Resps.json(request, Json.toFormatedJson(ips.values()));
//			return ret;
//		} catch (Exception e) {
//			log.error(e.toString(), e);
//			return Resps.json(request, RespVo.fail());
//		}
//	}
	
	
}
